<%-- 
    Document   : ProcessComposite
    Created on : 19  พ.ค   54
    Author     : Mr.sarunyoo keawsopa
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"
	errorPage="../error.jsp"%>

<%@page import="java.sql.*"%>

<%@page import="df.bean.process.ProcessComposit" %>
<%@page import="df.jsp.Guard"%>
<%@page import="df.jsp.LabelMap"%>
<%@page import="df.bean.db.conn.DBConnection"%>
<%@page import="df.bean.obj.util.Utils"%>
<%@page import="df.bean.db.table.TRN_Error"%>
<%@page import="df.bean.obj.util.JDate"%>
<%@page import="df.bean.db.table.Batch"%>
<%@ include file="../../_global.jsp"%>

<%
	//
	// Verify permission
	//

	if (!Guard.checkPermission(session, Guard.PAGE_PROCESS_DEMO)) {
		response.sendRedirect("../message.jsp");
		return;
	}

	//
	// Initial LabelMap
	//

	if (session.getAttribute("LANG_CODE") == null) {
		session.setAttribute("LANG_CODE", LabelMap.LANG_EN);
	}

	LabelMap labelMap = new LabelMap(session.getAttribute("LANG_CODE")
			.toString());

	labelMap.add("TITLE_MAIN", "Calculate Composite",
			" คำนวณรายการที่มีรายการสิทธิ์ร่วม");
	labelMap.add("START_DATE", "Start Date", "วันที่เริ่มต้น");
	labelMap.add("END_DATE", "End Date", "วันที่สิ้นสุด");
	labelMap.add("COL_0", "No.", "ลำดับ");
	labelMap.add("COL_1", "Process", "ขบวนการ");
	labelMap.add("COL_2", "Status", "สถานะ");

	request.setAttribute("labelMap", labelMap.getHashMap());

	String startDateStr = JDate.showDate(JDate.getDate());
	String endDateStr = JDate.showDate(JDate.getDate());
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>${labelMap.TITLE_MAIN}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../../css/share.css" media="all" />
<link type="text/css" href="../../css/themes/custom-theme/jquery.ui.all.css" rel="stylesheet" media="screen"/>
<script type="text/javascript" src="../../javascript/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../../javascript/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="../../javascript/util.js"></script>
<script type="text/javascript" src="../../javascript/ajax.js"></script>
<script type="text/javascript">
		$(document).ready(function(){ 
			$( "#START_DATE" ).datepicker({
				//showOn: "button",
				//buttonImage: "../../images/calendar_button.png",
				changeMonth: true,
				changeYear: true,
				buttonImageOnly: true,
				dateFormat: 'dd/mm/yy'
			}); 
			$( "#END_DATE" ).datepicker({
				//showOn: "button",
				//buttonImage: "../../images/calendar_button.png",
				changeMonth: true,
				changeYear: true,
				buttonImageOnly: true,
				dateFormat: 'dd/mm/yy'
			});				
		});
           function  AJAX_Send_Request_Composite() { 

        	   if (currentRowID <= toRowID ) {

                 table.rows[currentRowID].cells[2].innerHTML = '<img src="../../images/processing_icon.gif"/>';
                 var target = "../../ProcessCompositeSrvl?date_start=" + document.mainForm.START_DATE.value+"&date_end=" + document.mainForm.END_DATE.value;

                 AJAX_Request(target, AJAX_Handle_Response);

        	   } else {
        	       alert("Process Composite  complete.");
                   document.mainForm.RUN.disabled = false;
                   document.mainForm.STOP.disabled = true;
                   document.mainForm.CLOSE.disabled = false;
               }
					
           }
            
            function AJAX_Handle_Response() {
                
                if (AJAX_IsComplete()) {

                    var xmlDoc = AJAX.responseXML;

                    
                    if (xmlDoc.getElementsByTagName("SUCCESS")[0] == null) {
						// alert(xmlDoc.getElementsByTagName("SUCCESS")[0]);
                       alert("Exception in update process");
                       return;
                    }
                    
                    if (xmlDoc.getElementsByTagName("SUCCESS")[0].firstChild.nodeValue == "0") {
                        table.rows[currentRowID].cells[2].innerHTML = '<img src="../../images/failed_icon.gif"/>';
                    }    
                    else {
                        table.rows[currentRowID].cells[2].innerHTML = '<img src="../../images/succeed_icon.gif"/>';
                        // alert("Process Table Mapping Complete");
                    }

                    document.getElementById("PROGRESS").innerHTML = (currentRowID - 1) + " / " + numRow;
                            
                    currentRowID++;
                    
                    AJAX_Send_Request_Composite();

                }
            }
               
            var fromRowID = 0;
            var toRowID = 0;
            var numRow = 0;
            var currentRowID = 0;

            var table = document.getElementById("dataTable"); 
            
            function RUN_Composite_Click() {

                table = document.getElementById("dataTable");

          		fromRowID = currentRowID = 2;
               
				toRowID = document.getElementById("dataTable").rows.length - 1;

                document.mainForm.RUN.disabled = true;
                document.mainForm.STOP.disabled = false;
                document.mainForm.CLOSE.disabled = true;
    	        document.getElementById("PROGRESS").innerHTML = (currentRowID - 1) + " / " + numRow;
                
                AJAX_Send_Request_Composite();

            }
            
            function STOP_Composite_Click() {
                toRowID = 0;
                document.getElementById("PROGRESS").innerHTML = "";
                document.mainForm.RUN.disabled = false;
                document.mainForm.STOP.disabled = true;
                document.mainForm.CLOSE.disabled = false;
                
                // Send Roll back message via AJAX here 
            }           

        </script>
</head>
<body>
<form id="mainForm" name="mainForm" method="post" action="###">
	<center>
	    <table width="800" border="0">
	        <tr><td align="left">
	        	<b><font color='#003399'><%=Utils.getInfoPage("ProcessComposite.jsp", labelMap.getFieldLangSuffix(), new DBConnection(""+session.getAttribute("HOSPITAL_CODE")))%></font></b>
	        </td></tr>
	    </table>
	</center>
<table class="form">
	<tr>
		<th colspan="4">${labelMap.TITLE_MAIN}</th>
	</tr>


	<tr>
		<td class="label"><label for="START_DATE">${labelMap.START_DATE}</label></td>
		<td class="input"><input type="text" value="<%=startDateStr%>"
			id="START_DATE" name="START_DATE" class="short"
			value="<%=request.getParameter("START_DATE") == null ? ""
					: request.getParameter("START_DATE")%>" />
		<td class="label"><label for="END_DATE">${labelMap.END_DATE}</label></td>
		<td class="input"><input type="text" value="<%=endDateStr%>"
			id="END_DATE" name="END_DATE" class="short"
			value="<%=request.getParameter("END_DATE") == null ? ""
					: request.getParameter("END_DATE")%>" />
	</tr>

	<tr>
		<th colspan="4" class="buttonBar"><input type="button" id="RUN"
			name="RUN" class="button" value="${labelMap.RUN}"
			onclick="RUN_Composite_Click()" disabled="disabled" /> <input
			type="button" id="STOP" name="STOP" class="button" value="Stop"
			onclick="STOP_Composite_Click()" disabled="disabled" /> <input
			type="button" id="CLOSE" name="CLOSE" class="button"
			value="${labelMap.CLOSE}"
			onclick="window.location='../process/ProcessFlow.jsp'" /></th>
	</tr>
</table>
<hr />
<table class="data" id="dataTable" name="dataTable">
	<tr>
		<th colspan="3" class="alignLeft">
		<div style="float: left;">${labelMap.TITLE_MAIN}</div>
		<div style="float: right;" id="PROGRESS" name="PROGRESS"></div>
		</th>
	</tr>
	<tr>
		<td class="sub_head"><%=labelMap.get("COL_0")%></td>
		<td class="sub_head"><%=labelMap.get("COL_1")%></td>
		<td class="sub_head"><%=labelMap.get("COL_2")%></td>
	</tr>
	<%
		int i = 1;
	%>
	<tr>
		<td class="row<%=i % 2%> alignCenter"><%="1"%></td>
		<td class="row<%=i % 2%> alignCenter"><%="Process  Composite"%></td>
		<td class="row<%=i % 2%> alignCenter"><span id="pic"> <img
			src="../../images/waiting_icon.gif" /> </span></td>
	</tr>
	
	<%
		i++;
	%>
	<script type="text/javascript">
                    numRow = <%=i - 1%>;
                    document.getElementById("PROGRESS").innerHTML = "0 / " + numRow;
</script>
</table>
<%
	if (i > 1) {
		out
				.print("<script type=\"text/javascript\">document.mainForm.RUN.disabled = false;</script>");
	}
%>
</form>
</body>
</html>
